<?php
/**
	PHPBB3 API Class
	Copyright (c) 2009 Eric Hokanson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
define('IN_PHPBB', TRUE);

require_once 'Zend/Db.php';

class Zend_PHPBB3
{
	private $_phpbb_db;
	
	function __construct($phpbb_root_path = '../phpBB3/') {
		try {
			if (!file_exists($phpbb_root_path . '/config.php') || !file_exists($phpbb_root_path . '/includes/constants.php'))
				throw new Exception("phpBB installation not found.");
			require $phpbb_root_path . '/config.php';
			require_once $phpbb_root_path . '/includes/constants.php';
			$this->_phpbb_db = Zend_Db::factory('Mysqli', array(
				'host'     => $dbhost,
				'username' => $dbuser,
				'password' => $dbpasswd,
				'dbname'   => $dbname
			));
			$this->_phpbb_db->getConnection();
		} catch (Zend_Db_Adapter_Exception $e) {
			throw new Exception("Could not access phpBB database: " . $e->getMessage());
		}
		$this->_phpbb_db->query("SET NAMES 'utf8'") or die("Cannot set utf-8"); // Set UTF-8 mode
	}
	
	/**
	* Gets a list of forums for the board
	*
	* @return array   Array of forum_id, forum_name pairs
	*/	
	function get_forums() {
		return $this->_phpbb_db->fetchAll("SELECT forum_id, forum_name FROM " . FORUMS_TABLE . " WHERE forum_type = 1");
	}

	/**
	* Gets number of comments on a particular forum topic
	*
	* @param  int   $topic_id     Topic ID
	* @return int   Number of replies to $topic_id or -1 if not found
	*/	
	function get_count($topic_id) {
		$result = $this->_phpbb_db->fetchOne("SELECT topic_replies FROM " . TOPICS_TABLE . " WHERE topic_id = ?", $topic_id);
		if (is_numeric($result))
			return (int)$result;
		else
			return -1;
	}
	
	/**
	* Creates a new topic
	*
	* @param  string   $subject    Topic subject
	* @param  string   $text       Topic text
	* @param  int      $forum_id   Forum ID
	* @return int      topic_id from database
	*/	
	function create_topic($subject, $text, $forum_id) {
		$this->_phpbb_db->beginTransaction();
		try {
			$this->_phpbb_db->insert(TOPICS_TABLE, array('forum_id' => $forum_id,
												  'topic_poster' => 2,
												  'topic_first_poster_name' => 'GCMS Bot',
												  'topic_last_poster_id' => 2,
												  'topic_last_poster_name' => 'GCMS Bot',
												  'topic_title' => $subject,
												  'topic_time' => time(),
												  'topic_last_post_time' =>	time()));
			$topic_id = $this->_phpbb_db->lastInsertId();
			$this->_phpbb_db->insert(TOPICS_POSTED_TABLE, array('user_id' => 2, 'topic_id' => $topic_id, 'topic_posted' => 1));
			$this->_phpbb_db->insert(POSTS_TABLE, array('topic_id' => $topic_id,
												 'forum_id' => $forum_id,
												 'poster_id' => 2,
												 'poster_ip' => $_SERVER['SERVER_ADDR'],
												 'post_time' => time(),
												 'post_subject' => $subject,
												 'post_text' => $text,
												 'post_checksum' => md5($text)));
			$post_id = $this->_phpbb_db->lastInsertId();
			$this->_phpbb_db->update(TOPICS_TABLE, array('topic_last_post_id' => $post_id, 'topic_first_post_id' => $post_id), 'topic_id = ' . $topic_id);
			$this->_phpbb_db->update(FORUMS_TABLE, array('forum_posts' => new Zend_Db_Expr('forum_posts+1'), 'forum_topics' => new Zend_Db_Expr('forum_topics+1'), 'forum_topics_real' => new Zend_Db_Expr('forum_topics_real+1'), 'forum_last_post_subject' => $subject, 'forum_last_post_time' => time(), 'forum_last_poster_name' => 'GCMS Bot'), 'forum_id =' . $forum_id);
			$this->_phpbb_db->commit();
		} catch (Exception $e) {
			$this->_phpbb_db->rollBack();
			throw new Exception("Could not create a new topic: " . $e->getMessage());
		}
		return $topic_id;
	}
	
	/**
	* Sets the approved flag on a topic
	*
	* @param  int      $topic_id  Topic ID
	* @param  boolean  $approved  If topic is approved
	* @return int      number of rows updated
	*/	
	function set_approved($topic_id, $approved = TRUE) {
		if ($approved)
			$n = $this->_phpbb_db->update(TOPICS_TABLE, array('topic_approved' => 1), 'topic_id = ' . $topic_id);
		else
			$n = $this->_phpbb_db->update(TOPICS_TABLE, array('topic_approved' => 0), 'topic_id = ' . $topic_id);
		return $n;
	}
	
	/**
	* Gets the approved flag on a topic
	*
	* @param  string   $topic_id  Topic ID
	* @return boolean  true if approved or false if not
	*/	
	function get_approved($topic_id) {
		return (boolean)$this->_phpbb_db->fetchOne("SELECT topic_approved FROM " . TOPICS_TABLE . " WHERE topic_id = ?", $topic_id);
	}
}
?>